package com.huike.mapdemo;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 注：地图Demo 需要在真机上运行
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MapView map_main;
    private AMap aMap;
    private ImageView iv_map_location;
    private ImageView iv_map_cut;
    private ImageView iv_map_addr;
    private ImageView iv_map_distance;
    private TextView tv_map_navigation;
    private TextView tv_map_night;
    private TextView tv_map_norm;
    private TextView tv_map_moon;
    private TextView tv_map_traffic;
    private LinearLayout ll_map_menu;
    private TextView tv_map_title;
    private Boolean isClick = false;// 视图切换开关
    private LatLng latLng2;
    private List<LatLng> latLngs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initClick();

        map_main.onCreate(savedInstanceState);//地图初始化 必写
        if (aMap == null) {
            aMap =   map_main.getMap();
        }


    }

    private void initClick() {


        //切换视图开关
        iv_map_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isClick){
                    isClick = false;

                    AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
                    alphaAnimation.setDuration(1000);
                    ll_map_menu.startAnimation(alphaAnimation);
                    ll_map_menu.setVisibility(View.GONE);


                }else {
                    isClick = true;
                    ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);
                    scaleAnimation.setDuration(1000);
                    scaleAnimation.setFillAfter(true);
                    ll_map_menu.startAnimation(scaleAnimation);
                    ll_map_menu.setVisibility(View.VISIBLE);

                }

            }
        });

        tv_map_navigation.setOnClickListener(this);
        tv_map_moon.setOnClickListener(this);
        tv_map_night.setOnClickListener(this);
        tv_map_traffic.setOnClickListener(this);
        tv_map_norm.setOnClickListener(this);
        iv_map_addr.setOnClickListener(this);//显示小车位置图标
        iv_map_location.setOnClickListener(this);//定位到2号小车位置
        iv_map_distance.setOnClickListener(this);//显示1234小车连接线

    }

    private void initView() {
        map_main = (MapView) findViewById(R.id.map_main);
        iv_map_location = (ImageView) findViewById(R.id.iv_map_location);
        iv_map_cut = (ImageView) findViewById(R.id.iv_map_cut);
        iv_map_addr = (ImageView) findViewById(R.id.iv_map_addr);
        iv_map_distance = (ImageView) findViewById(R.id.iv_map_distance);
        tv_map_navigation = (TextView) findViewById(R.id.tv_map_navigation);
        tv_map_night = (TextView) findViewById(R.id.tv_map_night);
        tv_map_norm = (TextView) findViewById(R.id.tv_map_norm);
        tv_map_moon = (TextView) findViewById(R.id.tv_map_moon);
        tv_map_traffic = (TextView) findViewById(R.id.tv_map_traffic);
        ll_map_menu = (LinearLayout) findViewById(R.id.ll_map_menu);
        tv_map_title = (TextView) findViewById(R.id.tv_map_title);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        map_main.onDestroy();//地图初始化 必写
    }

    @Override
    protected void onResume() {
        super.onResume();
        map_main.onResume();//地图初始化 必写
    }

    @Override
    protected void onPause() {
        super.onPause();
        map_main.onPause();//地图初始化 必写
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);

        map_main.onSaveInstanceState(outState);
    }

    @Override
    public void onClick(View view) {

        if(view == tv_map_navigation){//导航
            aMap.setMapType(AMap.MAP_TYPE_NAVI);
            tv_map_title.setText("已切换到导航视图");
        }else if(view == tv_map_night){//夜景
            aMap.setMapType(AMap.MAP_TYPE_NIGHT);
            tv_map_title.setText("已切换到夜景视图");
        }else if(view == tv_map_norm){//标准
            aMap.setMapType(AMap.MAP_TYPE_NORMAL);
            tv_map_title.setText("已切换到标准视图");
        }else if(view == tv_map_moon){//卫星
            tv_map_title.setText("已切换到卫星视图");
            aMap.setMapType(AMap.MAP_TYPE_SATELLITE);
        }else  if(view == tv_map_traffic){//交通
            tv_map_title.setText("已切换到交通视图");
            aMap.setMapType(AMap.MAP_TYPE_BUS);
        }else if(view == iv_map_addr){//显示小车位置图标

            LatLng latLng1 = new LatLng(39.941853,116.385307);
            latLng2 = new LatLng(39.941003,116.105307);
            LatLng latLng3 = new LatLng(39.014453,116.300007);
            LatLng latLng4 = new LatLng(39.903523,116.305207);

            latLngs = new ArrayList<>();
            latLngs.add(latLng1);
            latLngs.add(latLng2);
            latLngs.add(latLng3);
            latLngs.add(latLng4);

            aMap.addMarker(new MarkerOptions().position(latLng1).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.marker_one))).title("一号小车\n联想大厦园区"));
            aMap.addMarker(new MarkerOptions().position(latLng2).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.marker_second))).title("二号小车\n联大想厦园区"));
            aMap.addMarker(new MarkerOptions().position(latLng3).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.marker_thrid))).title("三号小车\n联想厦大园区"));
            aMap.addMarker(new MarkerOptions().position(latLng4).icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.marker_forth))).title("四号小车\n想大联厦园区"));

            tv_map_title.setText("1，2，3，4号小车已完成标记");


        }else if(view == iv_map_location){//定位到二号小车位置

            //判断2号小车位置是否已经显示
            if(latLng2 == null){
                Toast.makeText(this, "需要先标识出2号小车的位置", Toast.LENGTH_SHORT).show();
                return;
            }

            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng2,12,0,0));
            aMap.moveCamera(cameraUpdate);
        }else  if(view == iv_map_distance){
            //连接4个小车位子线条
            aMap.addPolyline(new PolylineOptions().color(Color.RED).addAll(latLngs));
            aMap.addCircle(new CircleOptions().strokeWidth(15).radius(1000).center(latLng2).strokeColor(Color.BLACK));
        }

    }
}
